package com.bianguo.android.functiondemo.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.NonNull;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

import com.bianguo.android.functiondemo.R;
import com.bianguo.android.functiondemo.utils.MLog;

public class RefreshLayout extends ViewGroup {

    private int mWidth, mHeight;
    private ViewDragHelper dragHelper;

    public RefreshLayout(Context context) {
        super(context);
        initView(context, null);
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public RefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context mContext, AttributeSet attrs) {
        dragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(@NonNull View view, int pointerId) {
                return view.getId() == R.id.custom_move;
            }

            @Override
            public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) {
                if (left > getWidth() - child.getMeasuredWidth()) {
                    left = getWidth() - child.getMeasuredWidth();
                } else if (left < 0) {
                    left = 0;
                }
                return left;
            }

            @Override
            public int clampViewPositionVertical(@NonNull View child, int top, int dy) {
                if (top < 0) {
                    top = 0;
                } else if (top > getHeight() - child.getMeasuredHeight()) {
                    top = getHeight() - child.getMeasuredHeight();
                }
                return top;
            }

            @Override
            public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {
//                super.onViewReleased(releasedChild, xvel, yvel);
                float x = releasedChild.getX();
                float y = releasedChild.getY();
                if (x < (getMeasuredWidth() / 2f - releasedChild.getMeasuredWidth() / 2f)) { // 0-x/2
//                    if (x < releasedChild.getMeasuredWidth() / 3f) {
//                        x = 0;
//                    } else if (y < (releasedChild.getMeasuredHeight() * 3)) { // 0-y/3
//                        y = 0;
//                    } else if (y > (getMeasuredHeight() - releasedChild.getMeasuredHeight() * 3)) { // 0-(y-y/3)
//                        y = getMeasuredHeight() - releasedChild.getMeasuredHeight();
//                    } else {
//                        x = 0;
//                    }
                    x = 0;
                } else { // x/2-x
//                    if (x > getMeasuredWidth() - releasedChild.getMeasuredWidth() / 3f - releasedChild.getMeasuredWidth()) {
//                        x = getMeasuredWidth() - releasedChild.getMeasuredWidth();
//                    } else if (y < (releasedChild.getMeasuredHeight() * 3)) { // 0-y/3
//                        y = 0;
//                    } else if (y > (getMeasuredHeight() - releasedChild.getMeasuredHeight() * 3)) { // 0-(y-y/3)
//                        y = getMeasuredHeight() - releasedChild.getMeasuredHeight();
//                    } else {
//                        x = getMeasuredWidth() - releasedChild.getMeasuredWidth();
//                    }
                    x = getMeasuredWidth() - releasedChild.getMeasuredWidth();
                }                // 移动到x,y
                dragHelper.smoothSlideViewTo(releasedChild, (int) x, (int) y);
                invalidate();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int childCount = getChildCount();
        if (childCount == 0) {
            setMeasuredDimension(0, 0);
            return;
        }
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            //高度累加，宽度取最大
            setMeasuredDimension(getChildMaxWidth(), getTotalHeight());
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mWidth, getTotalHeight());
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(getChildMaxWidth(), mHeight);
        } else {
            setMeasuredDimension(mWidth, mHeight);
        }
    }

    private int getChildMaxWidth() {
        int maxWidth = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            int measuredWidth = childView.getMeasuredWidth();
            if (measuredWidth > maxWidth) {
                maxWidth = measuredWidth;
            }
        }
        return maxWidth;
    }

    private int getTotalHeight() {
        int childCount = getChildCount();
        int height = 0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            height += childView.getMeasuredHeight();
        }
        return height;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int childCount = getChildCount();
        int currentHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            int childHeight = childView.getMeasuredHeight();
            childView.layout(left, currentHeight, right, currentHeight + childHeight);
            currentHeight += childHeight;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            dragHelper.cancel();
            return false;
        }
        return dragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        dragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    public void computeScroll() {
        if (dragHelper.continueSettling(true)) {
            invalidate();
        }
    }
}
